//
//  XDMiyoshiBankErrProblemViewController.m
//  XDEducation
//
//  Created by 星道 on 17/5/13.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDMiyoshiBankErrProblemViewController.h"
#import "XDErrProblemHttpModel.h"
#import "XDProblemTimeCell.h"
#import "XDProblemTimeDetailCell.h"
#import "XDErrProlblemNetWork.h"
#import "XDKeWaiCell.h"
#import "XDProblemRecordViewController.h"
#import "WebViewController.h"
#import "HNViewController.h"
@interface XDMiyoshiBankErrProblemViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    
    IBOutlet UITableView *_LeftTableView;
    IBOutlet UITableView *_RightTableView;
    
}
/** 左侧表静态数据 */
@property (nonatomic, strong) NSArray *LeftDataArr;
/** 右侧动态数据 */
//时间
@property (nonatomic, strong) NSMutableArray *RightTimeDataArr;
@property (nonatomic, strong) NSMutableArray *RightDetailDtaArr;
/** 记录左侧单击行数 */
@property (nonatomic, assign) NSInteger LeftIndex;
@end

@implementation XDMiyoshiBankErrProblemViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    
    self.ModelType = 0;
    self.LeftIndex = 0;
    self.RightTableViewType = 0;
    
    
    //TableView配置
    [self ConfigTableView];
    
    [self RefreshTimeData];
}



#pragma mark - Config
/** TableView配置 */
- (void)ConfigTableView
{
    _LeftTableView.tableHeaderView = [UIView new];
    _LeftTableView.tableFooterView = [UIView new];
    _RightTableView.tableHeaderView = [UIView new];
    _RightTableView.tableFooterView = [UIView new];
    
    NSIndexPath*indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
    [_LeftTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionTop];
    
    [_RightTableView registerNib:[UINib nibWithNibName:[XDProblemTimeCell description] bundle:nil] forCellReuseIdentifier:@"TimeCell"];
    [_RightTableView registerNib:[UINib nibWithNibName:[XDProblemTimeDetailCell description] bundle:nil] forCellReuseIdentifier:@"TimeDetailCell"];
    [_RightTableView registerNib:[UINib nibWithNibName:[XDKeWaiCell description] bundle:nil] forCellReuseIdentifier:@"KeWaiCell"];
}


#pragma mark - 查询时间NetWork
//网络请求
- (void)RefreshTimeData
{
    //查询时间列表
    if (self.RightTableViewType == Time)
    {
        switch (self.LeftIndex)
        {
            case KeQian:[self NetWork_TimeType];break;
            case KeTang:[self NetWork_TimeType];break;
            case ZuoTi:break;
            default:[self NetWork_Time];break;
        }
    }
    
    //查询指定的时间内容
    if  (self.RightTableViewType == TimeDetail)
    {
        switch (self.ModelType)
        {
            case ZuoTi:;break;
            default:[self NetWork_TimeDetail];break;
        }
    }
    
    //显示指定时间内容的详情
    if (self.RightTableViewType == EnterDetail)
    {
        switch (self.ModelType)
        {
            case FuXi:[self NetWorkGetFuxiDetail];break;
            default:[self NetWorkKeWaiDetail];break;
        }
    }
}

//获取时间
- (void)NetWork_Time
{
    XDErrProblemHttpModel *Model = self.LeftDataArr[self.LeftIndex];
    [MBProgressHUD showHUD];
    [XDErrProlblemNetWork QueryForTime:Model Success:^(id Obj) {
        
        [self.RightTimeDataArr addObjectsFromArray:Obj];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
            
            [_RightTableView reloadData];
        });
        
    } Failure:^(NSError *Error) {
        [MBProgressHUD hideHUD];
        
        [MBProgressHUD showError:XDNetWorkError];
    }];
}

//获取课前预习、课堂小测的时间
- (void)NetWork_TimeType
{
    XDErrProblemHttpModel *Model = self.LeftDataArr[self.LeftIndex];
    [MBProgressHUD showHUD];
    [XDErrProlblemNetWork QueryTypeForTime:Model Success:^(id Obj) {
        
        [self.RightTimeDataArr addObjectsFromArray:Obj];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
            
            [_RightTableView reloadData];
        });
        
    } Failure:^(NSError *Error) {
        [MBProgressHUD hideHUD];
        
        [MBProgressHUD showError:XDNetWorkError];
    }];
}



//获取指定时间的内容
- (void)NetWork_TimeDetail
{
    XDErrProblemHttpModel *Model = self.LeftDataArr[self.ModelType];
    [MBProgressHUD showHUD];
    [XDErrProlblemNetWork QueryForTime_Detail:Model Success:^(id Obj) {
        
        [self.RightTimeDataArr removeAllObjects];
        
        if (Obj)
        {
            if (self.ModelType == FuXi)
            {
                for (int i=0; i<[Obj count]; i++)
                {
                    NSDictionary *dict = Obj[i];
                    
                    XDErrProblemFuXi *Model = [[XDErrProblemFuXi alloc] init];
                    [Model SetDict:dict];
                    XDErrProblemHttpModel *HttpModel = self.LeftDataArr[self.ModelType];
                    Model.Querytime = HttpModel.Querytime;
                    [self.RightTimeDataArr addObject:Model];
                }
            }
            else if (self.ModelType == KeWai)
            {
                for (NSDictionary *dict in Obj)
                {
                    XDErrProblemKeWai *Model = [[XDErrProblemKeWai alloc] init];
                    [Model SetDict:dict];
                    [self.RightTimeDataArr addObject:Model];
                }
            }
            else if (self.ModelType == QuanNeng)
            {
                NSArray *Arr = nil;
                if ([Obj[@"code"] isEqualToNumber:@1000] && [Obj[@"t"] count] > 0)
                {
                    Arr = (NSArray *)Obj[@"t"];
                    for (NSDictionary *dict in Arr)
                    {
                        XDErrProblemQuanNeng *Model = [[XDErrProblemQuanNeng alloc] init];
                        [Model SetDict:dict];
                        [self.RightTimeDataArr addObject:Model];
                    }
                }
                else
                {
                    [MBProgressHUD showError:Obj[@"data"][@"msg"]];
                }
            }
            else if (self.ModelType == KeQian)
            {
                for (NSDictionary *dict in Obj)
                {
                    XDErrProblemKeQian *Model = [[XDErrProblemKeQian alloc] init];
                    [Model SetDict:dict];
                    [self.RightTimeDataArr addObject:Model];
                }
            }
            else if (self.ModelType == KeTang)
            {
                for (NSDictionary *dict in Obj)
                {
                    XDErrProblemKeTang *Model = [[XDErrProblemKeTang alloc] init];
                    [Model SetDict:dict];
                    [self.RightTimeDataArr addObject:Model];
                }
            }
            
        }
        
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
            
            [_RightTableView reloadData];
        });
    } Failure:^(NSError *Error) {
        [MBProgressHUD hideHUD];
        
        [MBProgressHUD showError:XDNetWorkError];
    }];
}

//Index:1100详情 1101重做
- (void)NetWorkGetFuxiDetail
{
    XDErrProblemHttpModel *Model = self.LeftDataArr[self.ModelType];
    XDErrProblemFuXi *FuXiModel = self.RightTimeDataArr[Model.Index];
    
    WebViewController *web = [WebViewController new];
    NSString *UrlStr = @"";
    NSString *Name = @"";
    if (FuXiModel.TypeNumber == 1100)
    {
        UrlStr = [NSString stringWithFormat:@"myresult.do?action=getdetailsbypointid&knowledge_id=%@&querytime=%@&uniqueKey=%@&uid=%@",FuXiModel.knowledge_id,FuXiModel.Querytime,FuXiModel.uniqueKey,[AccountTool shareAccountTool].currentAccount.uid];
        Name = @"详情";
    }
    else if (FuXiModel.TypeNumber == 1101)
    {
        UrlStr = [NSString stringWithFormat:@"exercise.do?action=getRandTenQuestionByKnowId&knowledgeid=%@",FuXiModel.knowledge_id];
        Name = @"重做";
    }
    web.str = BaseURL(UrlStr);
    web.nameTitle = Name;
    web.Back = ^()
    {
        self.RightTableViewType = TimeDetail;
        self.ModelType = FuXi;
    };
    [self.navigationController pushViewController:web animated:YES];
}

- (void)NetWorkKeWaiDetail
{
    XDErrProblemHttpModel *Model = self.LeftDataArr[self.ModelType];
    NSString *UrlStr = @"";
    if (self.ModelType == KeWai)
    {
        XDErrProblemKeWai *KeWaiModel = self.RightTimeDataArr[Model.Index];
        UrlStr = [NSString stringWithFormat:@"exercise.do?action=getDetailsByHwid&uid=%@&subject_id=%@&hw_id=%@",[AccountTool shareAccountTool].currentAccount.uid,[KeWaiModel.subjectid stringValue],[KeWaiModel.hw_id stringValue]];
    }
    else if (self.ModelType == QuanNeng)
    {
        XDErrProblemQuanNeng *KeWaiModel = self.RightTimeDataArr[Model.Index];
        UrlStr = [NSString stringWithFormat:@"exercise.do?action=getStrengthDetailsByPaperId&uid=%@&uniqueKey=%@&paperId=%@",[AccountTool shareAccountTool].currentAccount.uid,KeWaiModel.uniqueKey,[KeWaiModel.paperId stringValue]];
    }
    else if (self.ModelType == KeQian)
    {
        XDErrProblemKeQian *KeWaiModel = self.RightTimeDataArr[Model.Index];
        UrlStr = [NSString stringWithFormat:@"exercise.do?action=getDetailsByHwid&uid=%@&hw_id=%@&subject_id=%@",[AccountTool shareAccountTool].currentAccount.uid,[KeWaiModel.hw_id stringValue],[KeWaiModel.subjectid stringValue]];
    }
    else if (self.ModelType == KeTang)
    {
        XDErrProblemKeTang *KeWaiModel = self.RightTimeDataArr[Model.Index];
        UrlStr = [NSString stringWithFormat:@"exercise.do?action=getDetailsByHwid&uid=%@&hw_id=%@&subject_id=%@",[AccountTool shareAccountTool].currentAccount.uid,[KeWaiModel.hw_id stringValue],[KeWaiModel.subjectid stringValue]];
    }
    WebViewController *web = [WebViewController new];
    web.str = BaseURL(UrlStr);
    web.nameTitle = @"详情";
    web.Back = ^()
    {
        self.RightTableViewType = TimeDetail;
        self.ModelType = KeWai;
    };
    [self.navigationController pushViewController:web animated:YES];
}


#pragma mark - TableViewViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _LeftTableView)
    {
        return [self.LeftDataArr count];
    }
    else
    {
        if (self.RightTableViewType == Time)
        {
            return [self.RightTimeDataArr count];
        }
        else if (self.RightTableViewType == TimeDetail)
        {
            return [self.RightTimeDataArr count];
        }
        else
        {
            return 10;
        }
        
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //左侧时间列表
    if (tableView == _LeftTableView)
    {
        UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"LeftCell"];
        if (!Cell)
        {
            Cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"LeftCell"];
            Cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[self ImgForRGB:CGSizeMake(ScreenW*2.5, 44) Color:ThemeColor]];
            Cell.textLabel.textColor = ThemeColor;
            Cell.textLabel.highlightedTextColor = UIColor(255, 255, 255);
            Cell.textLabel.font = [UIFont systemFontOfSize:14.f];;;
        }
        XDErrProblemHttpModel *Model = self.LeftDataArr[indexPath.row];
        Cell.textLabel.text = Model.Name;
        return Cell;
    }
    else
    {
        //右侧内容列表
        if (self.RightTableViewType == Time)
        {
            /** 右侧时间列表 */
            XDProblemTimeCell *TimeCell = [tableView dequeueReusableCellWithIdentifier:@"TimeCell" forIndexPath:indexPath];
            TimeCell.Time.text = self.RightTimeDataArr[indexPath.row];
            return TimeCell;
        }
        else if (self.RightTableViewType == TimeDetail)
        {
            /** 指定时间页 */
            if (self.ModelType == FuXi)
            {
                XDProblemTimeDetailCell *DetailCell = [tableView dequeueReusableCellWithIdentifier:@"TimeDetailCell" forIndexPath:indexPath];
                DetailCell.Model = self.RightTimeDataArr[indexPath.row];
                DetailCell.ClickButton = ^(UIButton *Button)
                {
                    //1100详情 1101重做
                    if ([Button.titleLabel.text isEqualToString:@"重做"])
                    {
                        for (XDErrProblemFuXi *Model in self.RightTimeDataArr)
                        {
                            Model.TypeNumber = 1101;
                        }
                        self.RightTableViewType = EnterDetail;
                        [self RefreshTimeData];
                    }
                    
                    if ([Button.titleLabel.text isEqualToString:@"消除盲点"])
                    {
                        HNViewController *VC = [[HNViewController alloc] init];
                        XDErrProblemFuXi *Model = self.RightTimeDataArr[indexPath.row];
                        VC.KnowledgeID = Model.knowledge_id;
                        [self.navigationController pushViewController:VC animated:YES];
                    }
                };
                return DetailCell;
            }
            else
            {
                XDKeWaiCell *KeWaiCell = [tableView dequeueReusableCellWithIdentifier:@"KeWaiCell" forIndexPath:indexPath];
                switch (self.ModelType)
                {
                    case KeWai:KeWaiCell.Model = self.RightTimeDataArr[indexPath.row];break;
                    case QuanNeng:KeWaiCell.QuanModel = self.RightTimeDataArr[indexPath.row];;break;
                    case KeQian:KeWaiCell.KeQianModel = self.RightTimeDataArr[indexPath.row];;break;
                    case KeTang:KeWaiCell.KeTangModel = self.RightTimeDataArr[indexPath.row];break;
                    default:break;
                }
                return KeWaiCell;
            }
        }
        else
        {
            return nil;
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //左侧列表单击记录
    if (tableView == _LeftTableView)
    {
        if (indexPath.row == 5)
        {
            //做题记录
            XDProblemRecordViewController *VC = [[XDProblemRecordViewController alloc] init];
            VC.title = @"做题记录";
            [self.navigationController pushViewController:VC animated:YES];
        }
        else
        {
            XDErrProblemHttpModel *Model = self.LeftDataArr[indexPath.row];
            Model.LeftIndex = self.ModelType = self.LeftIndex = indexPath.row;
            self.RightTableViewType = Time; //网络请求数据的对象改成时间列表
            [self.RightTimeDataArr removeAllObjects];
            [_RightTableView reloadData];
            [self RefreshTimeData];
        }
    }
    else
    {
        //如果单击的列表是时间列表
        if (_RightTableViewType == Time)
        {
            self.RightTableViewType = TimeDetail;
            XDErrProblemHttpModel *Model = self.LeftDataArr[self.ModelType];
            Model.Querytime = self.RightTimeDataArr[indexPath.row];
            Model.Index = indexPath.row;
            [self RefreshTimeData];
        }else if (self.RightTableViewType == TimeDetail)
        {
            if (self.ModelType == FuXi)
            {
                //1100详情 1101重做
                for (XDErrProblemFuXi *Model in self.RightTimeDataArr)
                {
                    Model.TypeNumber = 1100;
                }
                XDErrProblemHttpModel *Model = self.LeftDataArr[self.ModelType];
                Model.Index = indexPath.row;
                self.RightTableViewType = EnterDetail;
                [self RefreshTimeData];
            }
            else
            {
                XDErrProblemHttpModel *Model = self.LeftDataArr[self.ModelType];
                Model.Index = indexPath.row;
                self.RightTableViewType = EnterDetail;
                [self RefreshTimeData];
            }
        }
        
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _RightTableView)
    {
        if (self.RightTableViewType == Time)
        {
            return 44;
        }
        else if (self.RightTableViewType == TimeDetail)
        {
            if (self.ModelType == FuXi)
            {
                return 120;
            }
            else
            {
                return 100;
            }
        }
        else
        {
            return 0;
        }
    }
    else
    {
        return 44;
    }
}





#pragma mark - Function
/** 颜色转图片 */
- (UIImage *)ImgForRGB:(CGSize)Size Color:(UIColor *)Color
{
    CGRect rect = CGRectMake(0, 0, Size.width, Size.height);
    UIGraphicsBeginImageContext(Size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, Color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}



#pragma mark - Setter
- (NSArray *)LeftDataArr
{
    if (!_LeftDataArr)
    {
        _LeftDataArr = [XDErrProblemHttpModel ErrProblemArr];
    }
    return _LeftDataArr;
}

- (NSMutableArray *)RightTimeDataArr
{
    if (!_RightTimeDataArr)
    {
        _RightTimeDataArr = [NSMutableArray array];
    }
    return _RightTimeDataArr;
}

- (NSMutableArray *)RightDetailDtaArr
{
    if (!_RightDetailDtaArr)
    {
        _RightDetailDtaArr = [NSMutableArray array];
    }
    return _RightDetailDtaArr;
}
@end
